package ru.ya.kolemik;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.List;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class TrayCommander implements InitializingBean {

	private Image image;
	
	private PopupMenu popupMenu;
	
	private TrayIcon trayIcon;
	
	private String tooltip = "TrayCommander";
	
	public void setCommands(List<TrayCommand> commands) {
		this.commands = commands;
	}

	private List <TrayCommand> commands;

	public void setTooltip(String tooltip) {
		this.tooltip = tooltip;
		if (trayIcon != null) {
			trayIcon.setToolTip(tooltip);
		}
	}

	public void setImageName(String imageName) {
		URL url = TrayCommander.class.getClassLoader().getResource((imageName == null)?"upload.png":imageName);
		image = Toolkit.getDefaultToolkit().getImage(url);
	}

	public void setPopupMenu(PopupMenu popupMenu) {
		this.popupMenu = popupMenu;
		if (trayIcon != null) {
			trayIcon.setPopupMenu(popupMenu);
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		trayIcon = new TrayIcon(image, tooltip);
		if (popupMenu != null) {
			trayIcon.setPopupMenu(popupMenu);
		}
		if (!validate()) {
			throw new RuntimeException("Invalid tray icon configuration");
		}
	}

	private boolean validate() {
		if (image == null) {
			return false;
		}
		if (tooltip == null) {
			return false;
		}
		if (trayIcon == null) {
			return false;
		}
		return true;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if (SystemTray.isSupported()) {
			BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("config.xml"));
			TrayCommander trayCommander = (TrayCommander)beanFactory.getBean("trayCommander");

			ActionListener exitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (JOptionPane.showConfirmDialog(new JOptionPane(),
							"Do you really want to exit - all running commands will be stopped too", "Exit?",
							JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
						// System.out.println("Exiting...");
						System.exit(0);
					}
				}
			};

			PopupMenu popup = new PopupMenu();
			
			for (TrayCommand command : trayCommander.getCommands()) {
				popup.add(new CommandMenu(command));
			}
			
			MenuItem defaultItem = new MenuItem("Exit");
			defaultItem.addActionListener(exitListener);
			popup.add(defaultItem);
			
			trayCommander.setPopupMenu(popup);
			trayCommander.trayIcon.setImageAutoSize(true);
			
			try {
				SystemTray.getSystemTray().add(trayCommander.trayIcon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
		} else {
			System.err.println("System tray is currently not supported.");
		}
	}

	public List<TrayCommand> getCommands() {
		return commands;
	}

}
